package cn.com.woxitech.dev.controller.api;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;

import cn.com.woxitech.dev.common.FilesUtils;
import cn.com.woxitech.dev.common.APIResultFactory;
import cn.com.woxitech.dev.common.EncryptionUtils;
import cn.com.woxitech.dev.common.JSONUtils;
import cn.com.woxitech.dev.common.SMSCodeUtils;
import cn.com.woxitech.dev.dao.gen.WmRoomMapper;
import cn.com.woxitech.dev.model.gen.WmLecture;
import cn.com.woxitech.dev.model.gen.WmLecturePage;
import cn.com.woxitech.dev.model.gen.WmRoom;
import cn.com.woxitech.dev.model.gen.WmRoomExample;
import cn.com.woxitech.dev.model.gen.WmUser;
import cn.com.woxitech.dev.service.AppService;
import cn.com.woxitech.dev.service.CatalogService;
import cn.com.woxitech.dev.service.LecturePageService;
import cn.com.woxitech.dev.service.LectureService;
import cn.com.woxitech.dev.service.OrganizationService;
import cn.com.woxitech.dev.service.PrivilegeService;
import cn.com.woxitech.dev.service.QiniuService;
import cn.com.woxitech.dev.service.RoomService;
import cn.com.woxitech.dev.service.SMSCodeService;
import cn.com.woxitech.dev.service.SettingService;
import cn.com.woxitech.dev.service.UserService;
import cn.com.woxitech.dev.service.WeixinService;

@RestController
@RequestMapping("/test")
public class TestLUYANController {
	@Autowired
	WeixinService weixinService;
	@Autowired
	SettingService settingService;
	@Autowired
	PrivilegeService privilegeService;
	@Autowired
	QiniuService qiniuService;
	@Autowired
	RoomService roomService;
	@Autowired
	LectureService lectureService;
	@Autowired
	LecturePageService lecturePageService;
	@Autowired
	UserService userService;
	@Autowired
	SMSCodeService smsCodeService;
	@Autowired
	OrganizationService organizationService;
	@Autowired
	CatalogService catalogService;	
	@Autowired
	AppService appService;
	
	
	@Autowired
	WmRoomMapper roomMapper;

	@RequestMapping(value="/addpptroomlive",method=RequestMethod.POST)
	public Object roomtype(@RequestBody String body) throws JsonProcessingException, IOException {
		try {
			Map<String, Object> mp = new HashMap<String, Object>();
			WmRoomExample roome = new WmRoomExample();
			roome.createCriteria().andLiveTypeEqualTo("2");
			List<WmRoom> rooms = roomMapper.selectByExample(roome);
			for(WmRoom room: rooms){
				String roomuuid = room.getUuid();
				WmLecture lecture = this.lectureService.getLectureByRoomUuid(roomuuid);
				if(lecture == null){
					System.out.println("'"+roomuuid+"', \n");
					continue;
				}
				List<WmLecturePage> lp_list = this.lecturePageService.getLecturePageByLectureUuid(lecture.getUuid());
				
				if(lp_list == null || lp_list.isEmpty()){
					continue;
				}
				
				boolean hasSmallLive = false;
				for(WmLecturePage lp : lp_list){
					if(lp.getType().equals("smalllive")){
						hasSmallLive = true;
					}
				}
				if(!hasSmallLive){
					
					
					
					Map<String, Object> stream_map = qiniuService.createQiniuStream();
					String rtmpstreamserver = (String)stream_map.get("publishurl");
					String key = (String)stream_map.get("streamid");
					String hlsstreamserver = (String)stream_map.get("playurl");

					
					WmLecturePage page = new WmLecturePage();
					page.setCreatorid(room.getCreatoruuid());
					page.setLectureid(lecture.getUuid());
					page.setNum(999);
					page.setPageurl(hlsstreamserver);
					page.setPublishid(key);
					page.setThumburl(lp_list.get(0).getPageurl());
					page.setPublishurl(rtmpstreamserver);
					page.setTime(new Date(System.currentTimeMillis()));
					page.setType("smalllive");
					this.lecturePageService.insertLecturePage(page);
				}
				
			}
			
			
			return APIResultFactory.success(mp);	
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return APIResultFactory.fail();	
		}
	}
	
	
	@RequestMapping(value="/wmuser",method=RequestMethod.POST)
	public Object wmuser(@RequestBody String body) throws JsonProcessingException, IOException {
		try {
			Map<String, Object> mp = new HashMap<String, Object>();
			
			JsonNode jsonNode = JSONUtils.toJson(body);

			
			List<WmUser> list = userService.getAllUsers();
			System.out.println("[");
			for(WmUser user : list){
				
				System.out.println("{");
				System.out.println("\"unionid\":"+"\""+user.getWxUnionid()+"\",");
				System.out.println("\"useruuid\":"+"\""+user.getUuid()+"\"");
				System.out.println("},");
			}
			System.out.println("]");
			//[{userid:xxxid},{},{}]
			
			return APIResultFactory.success(mp);	
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return APIResultFactory.fail();	
		}
	}
	

	
	@RequestMapping(value="/roomcatalog",method=RequestMethod.POST)
	public Object roomcatalog(@RequestBody String body) throws JsonProcessingException, IOException {
		try {
			Map<String, Object> mp = new HashMap<String, Object>();
			
			FilesUtils f1 = FilesUtils.getInstance("catalogroom.json");
			String f1str = f1.readContents();
			JSONArray json = new JSONArray(f1str);
			int size = json.length();

			for(int i =0 ;i< size; i++){
				
				String roomuuid =((JSONObject)json.get(i)).getString("roomuuid");
				String cataloguuid =((JSONObject)json.get(i)).getString("cataloguuid");
				
				this.catalogService.insertCatalog_Room(cataloguuid, roomuuid);
			}
			
			
			return APIResultFactory.success(mp);	
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return APIResultFactory.fail();	
		}
	}
	
	
}
